import Vue from 'vue'
import VueRouter from 'vue-router'
import Article from '@/views/article'
import Layout from '@/views/layout'
import Login from '@/views/login'
import Dashboard from '@/views/dashboard'
// import { getToken } from '@/utils/storage'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      children: [
        { path: '/article', component: Article },
        { path: '/dashboard', component: Dashboard }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  // 如果是去登录页或者携带了token的，直接放行，否则拦截到登录页
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})

export default router
